/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package functions;

import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import model.Catalog;
import model.Database;
import model.Diagnose;
import model.Operation;

/**
 *
 * @author JArik
 */
public class Operations {

    @Deprecated
    public static int addOtherToCatalog(Operation oper) {
        return 0;
    }

    public static int addToCatalog(Operation oper) throws SQLException {
        if(oper.getName().isEmpty())
            throw new SQLException("Название операции не может быть пустым");
        String what ="name";
        String values ="'"+oper.getName() +"'";
        return Catalog.addOperation(what, values);
    }


    public static ArrayList<Operation> getByName(String name) throws SQLException
    {
        Database db = new Database();
        ResultSet set = db.getDBInfo("id, name", "operations",
                "name LIKE '%"+name+"%' AND deleted=0 ORDER BY name ASC ");
        ArrayList<Operation> list = new ArrayList<Operation>();
        while ( set.next())
        {
            Operation oper = new Operation();
            oper.setId(set.getInt(1));
            oper.setName(set.getString(2));
            list.add(oper);

        }
        return list;
    }

    public static int addOperation(int operId, double sellPrice, double payed,
                                    Date vizitDate, String descr, int visId,
                                    int dogId, int docId) throws SQLException
    {
        Database db = new Database();
        
        int res=0;
        if(visId <= 0)
            throw new SQLException("Неправельный номер визита");
        if(docId <= 0)
            throw new SQLException("Неправельный врач");
        if(vizitDate == null)
            throw new SQLException("Неправельная дата визита");
        if(operId <=0)
            throw new SQLException("Неправельный номер диагноза");
        if(dogId <= 0)
            throw new SQLException("Неправельный номер животного");

        vizitDate = new Date(vizitDate.getTime());
        ResultSet set = db.insertDBInfo("operations_made",
                "visitId,dogId,operId,exec,descr,date,price,payed",
                ""+visId+","+dogId+","+operId+","+docId+",'"+
                descr+"','"+vizitDate+"',"+sellPrice+","+payed );
        if(set.next())
            res= set.getInt(1);
        db.closeStatement();
        db=null;
        return res;
    }
    public static int updateOperation(int disId,
                                      int visId, int operId, double sellPrice,
                                      double payed,Date vizitDate, String descr,
                                      int dogId, int docId, boolean deleted)
                                      throws SQLException
    {
        Database db = new Database();
        
        if(disId <= 0)
            throw new SQLException("Неправельный идентификатор.");
        if(visId <= 0)
            throw new SQLException("Неправельный номер визита");
        if(docId <= 0)
            throw new SQLException("Неправельный врач");
        if(vizitDate == null)
            throw new SQLException("Неправельная дата визита");
        if(operId <=0)
            throw new SQLException("Неправельный номер диагноза");
        if(dogId <= 0)
            throw new SQLException("Неправельный номер животного");

        vizitDate = new Date(vizitDate.getTime());
        int res = db.updateDBInfo("operations_made",
                "visitId="+visId+","+
                "dogId="+dogId+","+
                "operId="+operId+","+
                "exec="+docId+","+
                "descr='"+descr+"',"+
                "date='"+vizitDate+"',"+
                "price="+sellPrice+","+
                "payed="+payed+","+
                "deleted="+(deleted == true ? 1:0)+" ",
                " disId="+disId);
        db.closeStatement();
        db=null;
        return res;
    }
}
